package engine.logic.hotKey
{
	import engine.data.ModelLocator;
	import engine.ui.lite.BaseUI;
	
	import flash.display.Stage;
	import flash.events.KeyboardEvent;
	import flash.text.TextField;
	
	import newx.StageInfo;

	/**
	 * 热键管理类 
	 * @author lw
	 * 
	 */
	public class HotKeyManage
	{
		private var _stage:Stage;
		
		private var _keyList:Array = [];
		
		private var _model:ModelLocator;
		
		private var _hotKeyList:HotKeyList;
		
		//是否启用
		private var _enabled:Boolean = true;
		
		public function HotKeyManage()
		{
			_stage = StageInfo.stage
			
			init();
		}
		
		private static var _self		:HotKeyManage;
		
		public static function getInstance():HotKeyManage
		{
			if (_self == null)
			{
				_self = new HotKeyManage();
			}
			
			return _self;
		}
		
		private function init():void
		{
			_model = ModelLocator.getInstance();
			
			onUse();
			
//			_model.addEventListener(HotKeyEvent.USE,onUse);
//			
//			_model.addEventListener(HotKeyEvent.INVALID,onInvalid);
			
			_hotKeyList = new HotKeyList();
		}
		
		private function onUse(e:HotKeyEvent =null):void
		{
//			_stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
//			_stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		private function onInvalid(e:HotKeyEvent):void
		{
//			_stage.removeEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
//			
//			_stage.removeEventListener(KeyboardEvent.KEY_UP, onKeyUp);
		}
		
		private function onKeyDown(e:KeyboardEvent):void{
			if(_model.scene.Role == null) return;
			if(!userHotKey())return;
			if(e.target is TextField)return;
			if(_keyList.indexOf(e.keyCode) == -1){
				_keyList.push(e.keyCode);
			}
			if(_keyList.length > 2)_keyList = [];
		}
		
		private function onKeyUp(e:KeyboardEvent):void{
			if(e.target is TextField)return;
			if(!userHotKey())return;
			_keyList.length > 1?autoAttack():openUI();
			_keyList = [];
		}
		
		private function autoAttack():void
		{	
			return;
			if(_keyList.length > 2)return;
			if(_hotKeyList.autoAttackList.indexOf(_keyList[0]) == -1)return;
			if(_hotKeyList.autoAttackList.indexOf(_keyList[1]) == -1)return;
			var hotkeyEvent:HotKeyEvent = new HotKeyEvent(HotKeyEvent.ATTACK_SKILL);
			hotkeyEvent._type = 10;
			_model.dispatchEvent(hotkeyEvent);
		}
		
		/**
		 * 选择了技能热键
		 **/
		private function clickSkill():Boolean
		{
			var hotkeyEvent:HotKeyEvent = new HotKeyEvent(HotKeyEvent.ATTACK_SKILL);
			//对应快捷键
			var type:int = -1;
			for each(var hotKey:Array in _hotKeyList.hotKeyAttackList){
				if(hotKey[0] == _keyList[0]){
					if(hotKey[1] != null){
						type = hotKey[1];
						break;
					}
				}
			}
			if(type >= 0){
				hotkeyEvent._type = type;
				_model.dispatchEvent(hotkeyEvent);
				return true;
			}
			return false;
		}
		
		/**
		 * 打开ui
		 **/
		private function openUI():void
		{
			if(clickSkill()){
				return;
			}
			for each(var hotKey:Array in _hotKeyList.hotKeyUIList){
				if(hotKey[0] == _keyList[0]){
					if(hotKey[1] != null&&(hotKey[1] is BaseUI)){
						var baseUI:BaseUI = (hotKey[1]);
						baseUI.parent == null?baseUI.show():baseUI.hide();
						break;
					}
				}
			}
		}
		
		/**
		 * 是否可以使用快捷键
		 */
		private function userHotKey():Boolean
		{
			//没角色就不能使用快捷键
			if(_model.scene.Role == null) return false;
			//新手场景不能使用快捷键
			
			return _enabled;
//			//特殊处理，在选择城堡成功前，不能使用快捷键功能
//			return !(_model.scene.Role.GetPropertiesEx("Level") <= 1 && _model.scene.Role.GetProperties("CastleNameID") == "");	
		}

		public function get enabled():Boolean
		{
			return _enabled;
		}

		public function set enabled(value:Boolean):void
		{
			_enabled = value;
		}
	}
}